from flask import Blueprint, render_template, request

from applications.common.curd import model_to_dicts
from applications.common.helper import ModelFilter
from applications.common.utils.http import table_api
from applications.common.utils.rights import authorize
from applications.common.utils.validate import xss_escape
from applications.common.utils.http import table_api, fail_api, success_api
from applications.models import MoniEquipListModels
from applications.schemas import MoniEquipListOutSchema
from applications.extensions import db
from datetime import datetime
from applications.models import MoniEquipListModels, AdminLog
from applications.schemas import UserOutSchema

monitor_equip_list_bp = Blueprint('MonitoEquipList', __name__, url_prefix='/admin/monitor/list')


# 用户管理
@monitor_equip_list_bp.get('/')
# @authorize("admin:monitor:equip", log=True)
def main():
    return render_template('admin/monitor_equip_list.html')


#   用户分页查询
@monitor_equip_list_bp.get('/data')
@authorize("admin:monitor:equip", log=True)
def data():
    # 获取请求参数
    equip_name = xss_escape(request.args.get('equip_name', type=str))
    equip_type = xss_escape(request.args.get('equip_type', type=str))
    equip_enable = xss_escape(request.args.get('equip_enable', type=int))
    equip_state = xss_escape(request.args.get('equip_state', type=str))
    id = request.args.get('id', type=int)
    # 查询参数构造
    mf = ModelFilter()
    if equip_name:
        mf.contains(field_name="equip_name", value=equip_name)
    if equip_type:
        mf.contains(field_name="equip_type", value=equip_type)
    if id:
        mf.exact(field_name="id", value=id)
    if equip_enable:
        mf.exact(field_name='equip_enable', value=equip_enable)
    if equip_state:
        mf.exact(field_name='equip_state', value=equip_state)
        # if tranning_state == 2:mf.exact(field_name='tranning_state_desc',value='已完成')
    # orm查询
    # 使用分页获取data需要.items
    user = MoniEquipListModels.query.filter(mf.get_filter(model=MoniEquipListModels)).layui_paginate()
    count = user.total
    # 返回api
    return table_api(data=model_to_dicts(schema=MoniEquipListOutSchema, data=user.items), count=count)


# 监测设备详情
@monitor_equip_list_bp.get('/details')
# @authorize("xxxxxxxxxxxxxxxxxxx", log=True)
def equip_list_details():
    return render_template('admin/monitor_equip_detail.html')


# 监测设备添加
@monitor_equip_list_bp.get('/adds')
# @authorize("xxxxxxxxxxxxxxxxxxx", log=True)
def equip_list_adds():
    return render_template('admin/monitor_equip_list_add.html')

# 监测设备保存
@monitor_equip_list_bp.post('/save')
# @authorize("aadmin:monitor:equip:add", log=True)
def save():
    req_json = request.json
    equip_name = xss_escape(req_json.get('equipName'))
    equip_type = xss_escape(req_json.get('equipType'))
    equip_state = xss_escape(req_json.get('equipSate'))
    equip_enable = '1'
    create_time = datetime.now().replace(microsecond=0)
    if not equip_name or not equip_type or not equip_state:
        return fail_api(msg="账号姓名密码不得为空")

    equipModel = MoniEquipListModels(equip_name=equip_name, equip_type=equip_type,equip_state=equip_state,equip_enable=equip_enable,create_time=create_time)
    db.session.add(equipModel)
    # roles = Role.query.filter(Role.id.in_(role_ids)).all()
    # for r in roles:
    #     user.role.append(r)
    db.session.commit()
    return success_api(msg="增加成功")